<template>
	<view class="content">
		<wx-open-launch-app id="launch-btn" appid="wx8fbbb431d64af37e" @error="handleErrorFn" @launch="handleLaunchFn"
			:extinfo="extInfo">
			<button class="btn">打开APP</button>
		</wx-open-launch-app>
	</view>
</template>
<script src="//cdn.jsdelivr.net/npm/eruda"></script>
<script>
	eruda.init();
</script>
<script>
	import jweixin from "@/static/jweixin-1.6.0.js";
	export default {
		data() {
			return {
				contentObj: {},
				extInfo: {
					path: "",
					extraData: {
						foo: "noBar"
					}
				},
			};
		},
		onLoad(e) {
			if (this.is_weixin()) {}

			this.postRequest();
			if (e.id) {
				this.extInfo.path = `/enterpriseServer/productInfo/index?id=${e.id}`;
			} else {
				this.extInfo.path = `/enterpriseServer/service/index`;
			}
		},
		methods: {
			postRequest() {
      let params = {
        url: window.location.href.split("#")[0],
      };
      var xhr = new XMLHttpRequest();
      xhr.open(
        "POST",
        "https://api.xiaozhucaishui.com/api/H5share/get_sign",
        true
      );
      xhr.setRequestHeader("Content-Type", "application/json");
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
          var data = JSON.parse(xhr.responseText);
          if (data.code === 1) {
            this.contentObj = data;
            this.authSDK(data);
          }
        } else if (xhr.readyState == 4) {
          console.error("Error:", xhr.status);
        }
      }.bind(this);
      xhr.send(JSON.stringify(params));
    },

			// 判断是否微信环境
			is_weixin() {
				let ua = navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == "micromessenger") {
					return true;
				} else {
					return false;
				}
			},
			authSDK(data) {
				console.log(data)
				let appId = data.appid
				let nonceStr = data.noncestr
				let signature = data.signature
				let timestamp = data.timestamp
				jweixin.config({
					debug: true,
					appId: appId,
					timestamp: timestamp,
					nonceStr: nonceStr,
					signature: signature,
					jsApiList: ["getLocation"],
					openTagList: ["wx-open-launch-app", "wx-open-launch-weapp"], // 这里要配置一下
				});
				jweixin.ready(function() {
					jweixin.checkJsApi({
						jsApiList: [
							"getLocation",
							"wx-open-launch-app",
							"wx-open-launch-weapp",
						], // 需要检测的 JS 接口列表，所有 JS 接口列表见附录2,
						success: function(res) {
							alert("checkJsApi success", res, "111111111111");
							console.log("wxJsApi success:", res);
						},
						fail: (err) => {
							alert("checkJsApi fail", err);
							console.log("wxJsApi fail:", err);
						},
					});
					// config信息验证后会执行 ready 方法，所有接口调用都必须在 config 接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在 ready 函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在 ready 函数中。
				});
			},
			handleErrorFn(e) {
				uni.showToast({
					title: "打开失败",
					icon: "success",
					mask: true,
				});
			},
			// 监听launch 函数
			handleLaunchFn(e) {
				uni.showToast({
					title: "跳转成功",
					icon: "success",
					mask: true,
				});
				console.log("跳转成功", JSON.stringify(e));
			},
		},
	};
</script>

<style></style>